//
//  OCSSRetailOrderDetail.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/11/1.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import HandyJSON
import SwiftyJSON

class OCSSRetailOrderDetail: OCSSBaseViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet var footerView: UIView!
    
    //FooterView
    @IBOutlet weak var totalPrice: UILabel!
    @IBOutlet weak var discount: UILabel!
    @IBOutlet weak var shouldPayLab: UILabel!
    @IBOutlet weak var payTypeLab: UILabel!
    @IBOutlet weak var orderTypeLab: UILabel!
    @IBOutlet weak var orderSourceLab: UILabel!
    @IBOutlet weak var distributionTypeLab: UILabel!
    @IBOutlet weak var customerNameLab: UILabel!
    @IBOutlet weak var customerPhoneLab: UILabel!
    @IBOutlet weak var salesmanLab: UILabel!
    @IBOutlet weak var paperNumLab: UILabel!
    @IBOutlet weak var createTimeLab: UILabel!
    
    var orderData:[OCSSRetailOrderDetailModel]?;
    
    var orderId:String?;
    var orderType:Int=0;
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "订单详情";
        self.regTableViewNib(tableView: tableView, className: OCSSRetailOrderDetailCell.className());
        tableView.tableFooterView = footerView;
        requestForData();
    }
    //MARK:网络请求
    func requestForData() -> Void {
        let params = ["Id":orderId,
                      "OrderType":String(orderType)];
        OCSSNetRequest.requestData(url: "/Order/OrderDetail_app", para: params as [String : Any] , type: MethodType.post, success: { (result) in
            let resDic = JSON(result).dictionaryValue;
            if JSON(resDic["Status"]!).boolValue {
                let orderInfo = JSON(resDic["Data"]!).dictionaryValue;
                self.setFooterView(orderInfo: orderInfo);
                let arr = JSON(resDic["Data"]!["OrderItemList"]).arrayObject! as NSArray;
                self.orderData = ([OCSSRetailOrderDetailModel].deserialize(from: arr))! as? [OCSSRetailOrderDetailModel];
                self.tableView.reloadData();
            }
        }) { (error) in
            print(error);
        }
        
    }
    func setFooterView(orderInfo: [String:JSON]) -> Void {
        totalPrice.text = "￥\(JSON(orderInfo["OrderTotalAmount"]!).stringValue)";
        discount.text = "￥\(JSON(orderInfo["DiscountAmount"]!).stringValue)";
        shouldPayLab.text = "￥\(JSON(orderInfo["IncomeAmount"]!).stringValue)";
        let payTypeCode = (JSON(orderInfo["PayType"]!).stringValue as NSString).intValue;
        var payTypeStr = "";
        
        switch payTypeCode {
        case 10:
            payTypeStr = "现金";
            break;
        case 20:
            payTypeStr = "微信";
            break;
        case 30:
            payTypeStr = "支付宝";
            break;
        case 60:
            payTypeStr = "转账";
            break;
        default:
            break;
        }
        payTypeLab.text = payTypeStr;
        let orderTypeCode = (JSON(orderInfo["OrderType"]!).stringValue as NSString).intValue;
        if orderTypeCode == 0 {
            orderTypeLab.text = "普通订单";
        }else{
            orderTypeLab.text = "批发订单";
        }
        let orderSourceCode = (JSON(orderInfo["OrderSource"]!).stringValue as NSString).intValue;
        var orderSourceStr = "";
        switch orderSourceCode {
        case 10:
            orderSourceStr = "服务店下单";
            break;
        case 20:
            orderSourceStr = "经销商导单";
            break;
        case 30:
            orderSourceStr = "渠道采购下单";
            break;
        case 40:
            orderSourceStr = "经纪人下单";
            break;
        default:
            orderSourceStr = "未知渠道";
            break;
        }
        orderSourceLab.text = orderSourceStr;
        
        let distributionCode = (JSON(orderInfo["DistributionType"]!).stringValue as NSString).intValue
        if distributionCode == 10 {
            distributionTypeLab.text = "现货";
        }else{
            distributionTypeLab.text = "订货";
        }
        customerNameLab.text = JSON(orderInfo["CustomerName"]!).stringValue;
        customerPhoneLab.text = JSON(orderInfo["LoginMobile"]!).stringValue
        salesmanLab.text = JSON(orderInfo["SalePersonName"]!).stringValue;
        paperNumLab.text = JSON(orderInfo["CustomerSysNo"]!).stringValue;
        createTimeLab.text = JSON(orderInfo["CreateDate"]!).stringValue
    }
    //MARK:UITableViewDelegate && UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (orderData?.count) ?? 0;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OCSSRetailOrderDetailCell") as! OCSSRetailOrderDetailCell;
        cell.model = self.orderData?[indexPath.row];
        
        return cell;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 74;
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
